package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceimpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public void save(SetmealDTO setmealDTO) {
        //保存套餐相关数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);
        //保存套餐菜品相关数据
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes.size()>0&&setmealDishes!=null){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
        }

        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteBath(List<Long> setmealIds) {
      //判断当前套餐能否被删除
        for(Long setmealId:setmealIds){
           Setmeal setmeal=setmealMapper.getById(setmealId);
           if(setmeal.getStatus()!=null&&setmeal.getStatus()==1){
               //当前套餐处于起售中不能被删除
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
           }else{
               //删除套餐数据
               setmealMapper.deleteById(setmealId);
               //删除套餐菜品关联数据
               setmealDishMapper.deleteBySetmealId(setmealId);
           }
        }


    }

    @Override
    public SetmealVO getById(Long id) {
        //根据Id查询套餐数据
         Setmeal setmeal=setmealMapper.getById(id);
         SetmealVO setmealVO=new SetmealVO();
         BeanUtils.copyProperties(setmeal,setmealVO);
         //根据Id查询套餐关联菜品数据
        List<SetmealDish> setmealDishes=setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //更新套餐数据
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        //更新套餐关联的菜品数据
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();

        if(setmealDishes.size()>0&&setmealDishes!=null){
            //删除旧数据
            setmealDishMapper.deleteBySetmealId(setmeal.getId());
            for(SetmealDish setmealDish:setmealDishes){
                setmealDish.setSetmealId(setmeal.getId());
            }
            //插入新数据
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    @Override
    public void startOrStop(Integer status, long id) {
        //如果套餐中包含有停售的菜品则该套餐不能起售
        if (status== StatusConstant.ENABLE){
           List<Dish> dishList=dishMapper.getBySetmealId(id);
           if (dishList.size()>0&&dishList!=null){
               for (Dish dish : dishList) {
                   if(dish.getStatus()==StatusConstant.DISABLE){
                       throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                   }
               }
           }
        }

        Setmeal setmeal=Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
